package com.good.child.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.good.child.MainActivity;
import com.good.child.R;
import com.good.child.bean.common.AppVersion;
import com.good.child.bean.login.UserInfoBean;
import com.good.child.config.Constants;
import com.good.child.http.CommonLogical;
import com.good.child.http.LoginLogical;
import com.good.child.ui.service.LocationUpdateService;
import com.good.child.ui.utils.CommonU;
import com.good.child.widgets.CancelInterface;
import com.good.child.widgets.UpdateNikeFragment;
import com.good.child.widgets.dialog.AppUpdateFragment;
import com.good.child.widgets.dialog.PreviewDialog;
import com.sjy.pickphotos.pickphotos.PhotoPicker;
import com.sjy.pickphotos.pickphotos.listeners.OnResultListener;
import com.yimidida.library.base.BaseActivity;
import com.yimidida.library.base.BasePresenter;
import com.yimidida.library.base.ICallBackResultListener;
import com.yimidida.library.base.LibraryApplication;
import com.yimidida.library.tool.TActivityUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AccountActivity extends BaseActivity implements CancelInterface {
    private static final int REQUEST_IMAGE = 2;
    private int maxNum = 1;
    private List<String> mSelectList;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.user_icon_iv)
    ImageView user_icon_iv;

    @Override
    protected void initView() {
        setStatusBar(false, true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setAndroidNativeLightStatusBar(this, true);
        setTitle("设置");
        mSelectList = new ArrayList<>();
        setName();
        setHeader();
    }

    public void setName() {
        String name = (String) LibraryApplication.getSharedata().getData(Constants.USER_NAME, "");
        tv_name.setText(name);
    }

    public void setHeader() {
        String header = (String) LibraryApplication.getSharedata().getData(Constants.USER_HEADER, "");
        Glide.with(AccountActivity.this).load(header).placeholder(getResources().getDrawable(R.mipmap.default_head)).into(user_icon_iv);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account);
        setStatusBar(true, false);
        setAndroidNativeLightStatusBar(this, true);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @OnClick({R.id.iv_edit, R.id.iv_add, R.id.user_icon_iv, R.id.btn_updata, R.id.btn_out})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.iv_edit:
                UpdateNikeFragment updateNameDialogFragment = new UpdateNikeFragment();
                updateNameDialogFragment.show(getSupportFragmentManager(), "update_nike");
                break;
            case R.id.iv_add:
                pickImage();
                break;
            case R.id.user_icon_iv:
                String header = (String) LibraryApplication.getSharedata().getData(Constants.USER_HEADER, "");
                PreviewDialog.showDialog(getSupportFragmentManager(),header,true);
                break;

            case R.id.btn_updata:
                checkUpdate();
                break;
            case R.id.btn_out:
                LocationUpdateService.stop();
                TActivityUtils.jumpToActivity(AccountActivity.this, LoginActivity.class);
                finish();
                break;
        }
    }

    @Override
    public void enterClick(String remark) {
        LoginLogical.getInstance().updateUserName(this, Constants.deviceCode, remark, new ICallBackResultListener() {
            @Override
            public void onCallBack(Object result) {
                boolean isSuccess = (boolean) result;
                if (isSuccess) {
                    ToastUtils.showLong("修改成功");
                    LibraryApplication.getSharedata().saveData(Constants.USER_NAME, remark);
                    tv_name.setText(remark);
                }
            }
        });
    }

    private void pickImage() {
        PhotoPicker.Album(this)
                .setMultiChooseSize(1)
                .setIsCompress(true)//是否压缩
                .setIsCrop(false)//是否裁切
                .setOnResultListener(new OnResultListener() {
                    @Override
                    public void onSucess(ArrayList<String> imagePathList) {
                        mSelectList.clear();
                        mSelectList.addAll(imagePathList);
                        updateHeaderImage();
                    }

                    @Override
                    public void onCancel() {

                    }
                }).start();
    }

    private void updateHeaderImage() {
        LoginLogical.getInstance().updateUserHeader(this, mSelectList, Constants.deviceCode, new ICallBackResultListener() {
            @Override
            public void onCallBack(Object result) {
                if (result == null) {
                    return;
                }
                UserInfoBean bean = (UserInfoBean) result;
                ToastUtils.showLong("修改成功");
                if (!TextUtils.isEmpty(bean.getAvatar())) {
                    LibraryApplication.getSharedata().saveData(Constants.USER_HEADER, bean.getAvatar());
                }
                Glide.with(AccountActivity.this).load(mSelectList.get(0)).into(user_icon_iv);
            }
        });
    }

    private void checkUpdate() {
        CommonLogical.getInstance().requestUpdateApp(this, new ICallBackResultListener() {
            @Override
            public void onCallBack(Object result) {
                if (result == null) {
                    return;
                }
                AppVersion appVersion = (AppVersion) result;
                int serverVersion = 0;
//                if (!TextUtils.isEmpty(appVersion.getClientVersion())) {
//                    serverVersion = Integer.parseInt(appVersion.getClientVersion());
//                }
                int versionCode = CommonU.getVersionCode(AccountActivity.this);
//                if (serverVersion <= versionCode) {
////                    SharePreferenceUitls.remove(getActivity(), AppVersionUpdatePresenter.LAST_REQUEST_TIME);
////                    SharePreferenceUitls.remove(getActivity(), AppVersionUpdatePresenter.IS_FORCE_UPDATE);
////                ToastUtils.ToastShortCenter(getActivity(), getActivity().getString(R.string.str_new_version));
//                    if (showTips) {
//                        ToastUtil.showShort("已是最新版本");
//                    }
//                    return;
//                }
                AppUpdateFragment.show(AccountActivity.this.getSupportFragmentManager(), appVersion);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            TActivityUtils.jumpToActivity(getBaseContext(), MainActivity.class);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
